import React from 'react';
import { Router, Route, IndexRoute } from 'dva/router';
import { Home, Dashboard, PublicNumber, WeChat, UserMsg, UserHistory } from './routes';
import { dashboard, userHistory, userMsg, weChat } from './models/index';

function RouterConfig({ history }) {
  return (
    <Router history={history}>

      <Route name="index" path="/" component={Home}>
        <IndexRoute component={Dashboard} />
        <Route name="Dashboard" path="/dashboard" component={Dashboard} models={dashboard} />
        <Route path="/publicNumber" component={PublicNumber} />
        <Route path="/UserMsg" component={UserMsg} models={userMsg} />
        <Route path="/UserMsg/UserHistory/:id" component={UserHistory} models={userHistory} />
        <Route path="/WeChat" component={WeChat} models={weChat} />
        {/* <Route  path="/publicNumber/Union" component={PublicNumber}/>*/}
        {/* <Route path="/union" component={Curd} />
        <Route path="/happyife" component={Curd} />
        <Route path="/IntelligentParadise" component={Curd} />*/}

      </Route>


    </Router>
  );
}


const cached = {};
function registerModel(app, model) {
  console.log(!cached[model.namespace]);
  if (!cached[model.namespace]) {
    app.model(model);
    cached[model.namespace] = 1;
  }
}

const Routers = function ({ history, app }) {
  const routes = [
    {
      path: '/',
      component: Home,
      getIndexRoute(nextState, cb) {
        require.ensure([], (require) => {
          registerModel(app, require('./models/dashboard'));
          cb(null, { component: Dashboard });
        }, 'dashboard');
      },
      childRoutes: [
        {
          path: 'Dashboard',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/dashboard'));
              cb(null, Dashboard);
            }, 'dashboard');
          },
        },
        {
          path: 'WeChat',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/weChat'));
              cb(null, WeChat);
            }, 'weChat');
          },
        },
        {
          path: 'UserMsg',
          getComponent(nextState, cb) {
            require.ensure([], (require) => {
              registerModel(app, require('./models/userMsg'));
              cb(null, UserMsg);
            }, 'userMsg');
          },
        },
      ],
    },
  ];
  return <Router history={history} routes={routes} />;
};


export default RouterConfig;

